home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / unix / c / dev < prev    next >
Text File  |  1996-11-09  |  7KB  |  412 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/dev,v $
  4.  * $Date: 1996/10/30 21:59:01 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: dev,v $
  10.  * Revision 1.2  1996/10/30 21:59:01  unixlib
  11.  * Massive changes made by Nick Burret and Peter Burwood.
  12.  *
  13.  * Revision 1.1  1996/04/19 21:35:27  simon
  14.  * Initial revision
  15.  *
  16.  ***************************************************************************/
  17.  
  18. static const char rcs_id[] = "$Id: dev,v 1.2 1996/10/30 21:59:01 unixlib Rel $";
  19.  
  20. #include <errno.h>
  21. #include <stdlib.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <sys/unix.h>
  25. #include <sys/param.h>
  26. #include <sys/dev.h>
  27. #include <sys/os.h>
  28.  
  29. #define IGNORE(x) x = x
  30.  
  31. struct dev __dev[NDEV] =
  32. {
  33.   {__fsopen, __fsclose, __fsread, __fswrite, __fslseek, __fsioctl},
  34.   {__ttyopen, __ttyclose, __ttyread, __ttywrite, __ttylseek, __ttyioctl},
  35. {__pipeopen, __pipeclose, __piperead, __pipewrite, __pipelseek, __pipeioctl},
  36. {__nullopen, __nullclose, __nullread, __nullwrite, __nulllseek, __nullioctl}
  37. };
  38.  
  39. int
  40. __fsopen (char *file, int mode, struct file *f)
  41.  
  42. {
  43.   register int oflag = f->oflag;
  44.   register int *r = f->r;
  45.   _kernel_oserror *e;
  46.   int fd;
  47.  
  48.   file = __uname (file, oflag & (O_CREAT | O_OMASK));
  49.  
  50.   if (e = os_file (0x05, file, r))
  51.     {
  52.       __seterr (e);
  53.       return (-1);
  54.     }
  55.  
  56.   if (r[0])
  57.     {
  58.       if ((oflag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
  59.     {
  60.       errno = EEXIST;
  61.       return (-1);
  62.     }
  63.       if (oflag & O_CREAT)
  64.     oflag &= ~O_CREAT;
  65.       if (oflag & O_OMASK)
  66.     if (e = os_file (0x09, file, r))
  67.       {
  68.         __seterr (e);
  69.         return (-1);
  70.       }
  71.     }
  72.   else
  73.     {
  74.       if ((oflag & (O_RDONLY | O_CREAT)) == O_RDONLY)
  75.     {
  76.       errno = ENOENT;
  77.       return (-1);
  78.     }
  79.       if (oflag & O_OMASK)
  80.     oflag |= O_CREAT;
  81.     }
  82.  
  83.   if (oflag & O_CREAT)
  84.     {
  85.       r[2] = (oflag & O_BINARY) ? 0xffd : 0xfff;
  86.       r[4] = r[5] = 0;
  87.       if (e = os_file (0x0b, file, r))
  88.     {
  89.       __seterr (e);
  90.       return (-1);
  91.     }
  92.  
  93.       mode &= ~(__u->umask & 0777);
  94.  
  95.       r[5] = ((mode & 0400) >> 8) | ((mode & 0200) >> 6) |
  96.     ((mode & 0004) << 2) | ((mode & 0002) << 4);
  97.  
  98.       if (e = os_file (0x04, file, r))
  99.     {
  100.       __seterr (e);
  101.       return (-1);
  102.     }
  103.     }
  104.  
  105.   if (e = os_file (0x05, file, r))
  106.     {
  107.       __seterr (e);
  108.       return (-1);
  109.     }
  110.  
  111.   if (oflag & O_TRUNC)
  112.     e = os_fopen (0x80, file, &fd);
  113.   else
  114.     switch (oflag & O_OMASK)
  115.       {
  116.       case O_RDONLY:
  117.     e = os_fopen (0x40, file, &fd);
  118.     break;
  119.       case O_WRONLY:
  120.       case O_RDWR:
  121.     e = os_fopen (0xc0, file, &fd);
  122.     break;
  123.       default:
  124.     errno = EINVAL;
  125.     return (-1);
  126.     break;
  127.       }
  128.  
  129.   if (e)
  130.     {
  131.       __seterr (e);
  132.       return (-1);
  133.     }
  134.  
  135.   return (fd);
  136. }
  137.  
  138. int
  139. __fsclose (int fd, struct file *f)
  140.  
  141. {
  142.   _kernel_oserror *e;
  143.  
  144.   IGNORE (f);
  145.  
  146.   if (e = os_fclose (fd))
  147.     {
  148.       __seterr (e);
  149.       return (-1);
  150.     }
  151.  
  152.   return (0);
  153. }
  154.  
  155. int
  156. __fsread (int fd, register void *data, register int nbyte, struct file *f)
  157.  
  158. {
  159.   int r[5];
  160.   _kernel_oserror *e;
  161.  
  162.   IGNORE (f);
  163.  
  164.   if (e = os_fread (fd, data, nbyte, r))
  165.     {
  166.       __seterr (e);
  167.       return (-1);
  168.     }
  169.  
  170.   return (nbyte - r[3]);
  171. }
  172.  
  173. int
  174. __fswrite (int fd, register void *data, register int nbyte, struct file *f)
  175.  
  176. {
  177.   int r[5];
  178.   _kernel_oserror *e;
  179.  
  180.   IGNORE (f);
  181.  
  182.   if (e = os_fwrite (fd, data, nbyte, r))
  183.     {
  184.       __seterr (e);
  185.       return (-1);
  186.     }
  187.  
  188.   return (nbyte - r[3]);
  189. }
  190.  
  191. __off_t
  192. __fslseek (int fd, __off_t lpos, int whence, struct file *f)
  193.  
  194. {
  195.   int r[3];
  196.   _kernel_oserror *e;
  197.  
  198.   IGNORE (f);
  199.  
  200.   switch (whence)
  201.     {
  202.     case 0:
  203.       if (e = os_args (1, fd, (int) lpos, r))
  204.     {
  205.       __seterr (e);
  206.       return (-1);
  207.     }
  208.       break;
  209.     case 1:
  210.       if (e = os_args (0, fd, 0, r))
  211.     {
  212.       __seterr (e);
  213.       return (-1);
  214.     }
  215.       if (e = os_args (1, fd, r[2] + (int) lpos, r))
  216.     {
  217.       __seterr (e);
  218.       return (-1);
  219.     }
  220.       break;
  221.     case 2:
  222.       if (e = os_args (2, fd, 0, r))
  223.     {
  224.       __seterr (e);
  225.       return (-1);
  226.     }
  227.       if (e = os_args (1, fd, r[2] + (int) lpos, r))
  228.     {
  229.       __seterr (e);
  230.       return (-1);
  231.     }
  232.       break;
  233.     default:
  234.       errno = EINVAL;
  235.       return (-1);
  236.       break;
  237.     }
  238.  
  239.   return ((long) r[2]);
  240. }
  241.  
  242. int
  243. __fsioctl (int fd, register int request, void *arg, struct file *f)
  244.  
  245. {
  246.   IGNORE (fd);
  247.   IGNORE (request);
  248.   IGNORE (arg);
  249.   IGNORE (f);
  250.   errno = EINVAL;
  251.   return (-1);
  252. }
  253.  
  254. struct pipe *__pipe = NULL;
  255.  
  256. int
  257. __pipeopen (char *file, int mode, struct file *f)
  258.  
  259. {
  260.   return (__fsopen (file, mode, f));
  261. }
  262.  
  263. int
  264. __pipeclose (int fd, struct file *f)
  265.  
  266. {
  267.   struct pipe *pi = __pipe, *pi_ = 0;
  268.  
  269.   while (pi)
  270.     {
  271.       if (pi->p[0] == f || pi->p[1] == f)
  272.     break;
  273.       pi_ = pi;
  274.       pi = pi->next;
  275.     }
  276.   if (!pi)
  277.     {
  278.       errno = EBADF;
  279.       return (-1);
  280.     }
  281.   if (pi_)
  282.     pi_->next = pi->next;
  283.   else
  284.     __pipe = pi->next;
  285.   __fsclose (fd, f);
  286.   {
  287.     _kernel_oserror *e;
  288.  
  289.     if (e = os_fsctrl (27, __uname (pi->file, 0), 0, 0642))
  290.       {
  291.     __seterr (e);
  292.     return (-1);
  293.       }
  294.   }
  295.   free (pi->file);
  296.   free (pi);
  297.   return (0);
  298. }
  299.  
  300. int
  301. __piperead (int fd, void *data, int nbyte, struct file *f)
  302.  
  303. {
  304.   if (f->oflag & O_PIPE)
  305.     {
  306.       f->oflag &= ~O_PIPE;
  307.       __fslseek (fd, 0, 0, f);
  308.     }
  309.   return (__fsread (fd, data, nbyte, f));
  310. }
  311.  
  312. int
  313. __pipewrite (int fd, void *data, int nbyte, struct file *f)
  314.  
  315. {
  316.   if (f->oflag & O_PIPE)
  317.     return (__fswrite (fd, data, nbyte, f));
  318.   else
  319.     {
  320.       errno = EPIPE;
  321.       return (-1);
  322.     }
  323. }
  324.  
  325. __off_t
  326. __pipelseek (int fd, __off_t lpos, int whence, struct file *f)
  327.  
  328. {
  329.   IGNORE (fd);
  330.   IGNORE (lpos);
  331.   IGNORE (whence);
  332.   IGNORE (f);
  333.   errno = ESPIPE;
  334.   return (-1);
  335. }
  336.  
  337. int
  338. __pipeioctl (int fd, register int request, void *arg, struct file *f)
  339.  
  340. {
  341.   IGNORE (fd);
  342.   IGNORE (request);
  343.   IGNORE (arg);
  344.   IGNORE (f);
  345.   errno = EINVAL;
  346.   return (-1);
  347. }
  348.  
  349. int
  350. __nullopen (char *file, int mode, struct file *f)
  351.  
  352. {
  353.   IGNORE (file);
  354.   IGNORE (mode);
  355.   IGNORE (f);
  356.   return (0);
  357. }
  358.  
  359. int
  360. __nullclose (int fd, struct file *f)
  361.  
  362. {
  363.   IGNORE (fd);
  364.   IGNORE (f);
  365.   return (0);
  366. }
  367.  
  368. int
  369. __nullread (int fd, void *data, int nbyte, struct file *f)
  370.  
  371. {
  372.   IGNORE (fd);
  373.   IGNORE (data);
  374.   IGNORE (nbyte);
  375.   IGNORE (f);
  376.   return (0);
  377. }
  378.  
  379. int
  380. __nullwrite (int fd, void *data, int nbyte, struct file *f)
  381.  
  382. {
  383.   IGNORE (fd);
  384.   IGNORE (data);
  385.   IGNORE (f);
  386.   return (nbyte);
  387. }
  388.  
  389. __off_t
  390. __nulllseek (int fd, __off_t lpos, int whence, struct file *f)
  391.  
  392. {
  393.   IGNORE (fd);
  394.   IGNORE (lpos);
  395.   IGNORE (whence);
  396.   IGNORE (f);
  397.   errno = ESPIPE;
  398.   return (-1);
  399. }
  400.  
  401. int
  402. __nullioctl (int fd, int request, void *arg, struct file *f)
  403.  
  404. {
  405.   IGNORE (fd);
  406.   IGNORE (request);
  407.   IGNORE (arg);
  408.   IGNORE (f);
  409.   errno = EINVAL;
  410.   return (-1);
  411. }
  412.